package com.example.vv.model.register.base.presenter;

import android.util.Log;

import com.example.vv.frameWork.entity.LoginEntity;
import com.example.vv.frameWork.entity.RegisterEntity;
import com.example.vv.frameWork.mvp.MvpPresenter;
import com.example.vv.frameWork.mvp.ViewISNullException;
import com.example.vv.model.register.base.contract.RegisterContract;
import com.example.vv.model.register.base.model.RegisterModel;
import com.example.vv.req.BaseRequest;
import com.example.vv.req.REQ_LOGIN;
import com.example.vv.res.BaseRespont;
import com.google.gson.Gson;

import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

public class RegisterPresenter extends MvpPresenter<RegisterContract.View, RegisterContract.Model> implements RegisterContract.Presenter {



    @Override
    public void attach(RegisterContract.View view) {
        super.attach(view);
        model = new RegisterModel();
    }

    @Override
    public void register() {
        try {
            REQ_LOGIN re = new REQ_LOGIN(view().getPhone(),view().getPassword());
            model.req_login(re)
                    .map(new Function<BaseRespont, RegisterEntity>() {
                        @Override
                        public RegisterEntity apply(BaseRespont baseRespont)  {
                            return new Gson().fromJson(baseRespont.getBaseData(),RegisterEntity.class);
                        }
                    }).subscribe(new Consumer<RegisterEntity>() {
                @Override
                public void accept(RegisterEntity registerEntity)  {
                    try {
                        view().setReqisterEntity(registerEntity);
                        Log.e("sdfasfsadf",  registerEntity.getMessage());
                    } catch (ViewISNullException e) {
                        e.printStackTrace();
                    }
                }
            },new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) {

                }
            });
        } catch (ViewISNullException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean isFormt() {
        try {
            return view().getPhone()!=null&&view().getPhone().length()==11&&view().getPassword().length()>6
                    && view().getCaptcha()!=null&&view().getPassword().equals(view().getRepassWord());
        } catch (ViewISNullException e) {
            e.printStackTrace();
            return false;
        }
    }
}
